<template>
  <el-dialog class="pick-dialog" title="编辑价格" :visible.sync="dialogVisible" :close-on-click-modal="false" width="600px">
    <div class="form-box">
      <el-form :model="ruleForm" ref="ruleForm" :inline="true" label-width="100px" :rules="rules" class="demo-ruleForm">
        <el-divider content-position="left">平台售价</el-divider>
        <el-form-item label="商品价格:" prop="platformAmount">
          <span>{{orderInfo.platformAmount}}</span>
        </el-form-item>

        <el-form-item label="改价后:" prop="saleAmount">
          <el-input :disabled="orderInfo.salePay==2" @change="computedActualAmount" v-model="ruleForm.saleAmount"></el-input>
        </el-form-item>
        <!-- <el-form-item label="销售折扣:" prop="">
         <span>{{ orderInfo.saleDiscount}}</span>
        </el-form-item> -->
        <el-form-item label="包赔费:" prop="">
          <span>{{ ruleForm.guaranteeAmount}}</span>
        </el-form-item>
        <el-form-item label="买家服务费:" prop="" v-if="orderInfo.type == 3">
          <span>{{ ruleForm.servicePrice}}</span>
        </el-form-item>
        <el-form-item label="实付款:" prop="actualAmount">
          <span>{{ ruleForm.actualAmount}}</span>
        </el-form-item>
        <el-divider content-position="left" v-if="orderInfo.orderType==1">成本</el-divider>
        
        <el-form-item label="卖家服务费:" prop="" v-if="orderInfo.type == 3">
          <span>{{ruleForm.sellerFee}}</span>
        </el-form-item>
        <el-form-item label="成本:" prop="" v-if="orderInfo.orderType==1">
          <span>{{ruleForm.supplyAmount}}</span>
        </el-form-item>
        <el-divider content-position="left" v-if="orderInfo.orderType==2">供货价</el-divider>
        <el-form-item label="原供货价:" prop="" v-if="orderInfo.orderType==2">
          <span>{{orderInfo.supplyPlatformAmount}}</span>
        </el-form-item>
        <el-form-item label="修改供货价:" prop="supplyAmount" v-if="orderInfo.orderType==2">
          <el-input @change="computedSupplyAmount" v-model="ruleForm.supplyAmount"></el-input>
        </el-form-item>
        <!-- <el-form-item label="供货折扣:" prop="">
         <span>{{ orderInfo.supplyDiscount}}</span>
        </el-form-item> -->
        <!-- <el-form-item label="实际结算价:" prop="supplyDiscountAmount">
         <span>{{ ruleForm.supplyDiscountAmount}}</span>
        </el-form-item> -->
      </el-form>

      <div class="center">
        <el-button @click="close('ruleForm')">关闭</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="loading">确认</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import orderApi from "@/api/pickUp/order";
import customerServiceApi from "@/api/pickUp/customerService";
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      ruleForm: {
        actualAmount: "",
        id: "",
        saleAmount: "",
        supplyDiscountAmount: "",
        supplyAmount: "",
        guaranteeAmount: "",
        servicePrice: "",
      },
      orderInfo: {},
      rules: {},
      baopeiSelectObj: "",
      serviceFeeConfig: {
        buyerRate: "",
        buyerMin: "",
        sellerRate: "",
        sellerMin: "",
      },
    };
  },
  created() {},
  methods: {
    open(order) {
      console.log("%c Line:84 🍞 order", "color:#42b983", order);
      this.loading = false;
      this.orderInfo = order;
      this.ruleForm.id = order.id;
      this.ruleForm.saleAmount = order.saleAmount || "";
      this.ruleForm.actualAmount = order.actualAmount||'';
      this.ruleForm.supplyDiscountAmount = order.supplyDiscountAmount||'';
      this.ruleForm.supplyAmount = order.supplyAmount||'';
      this.ruleForm.servicePrice = order.servicePrice||'';
      this.ruleForm.guaranteeAmount = order.guaranteeAmount||'';
      this.ruleForm.supplyServicePrice = order.supplyServicePrice||'';
      this.dialogVisible = true;
      this.getSupplyGuaranteeConfig();
      this.getServiceFeeConfig();
    },
    // 获取供货商的包赔配置 customerServiceApi
    getSupplyGuaranteeConfig() {
      this.baopeiSelectObj = "";
      customerServiceApi
        .getConfigByTenantId({
          configKey: "guaranteeConfig",
        })
        .then((res) => {
          if (
            res.data &&
            res.data.configValue &&
            !res.data.configValue.includes("==")
          ) {
            const baopeiList = JSON.parse(res.data.configValue);
            if (baopeiList && baopeiList.length) {
              baopeiList.forEach((i) => {
                if (i.name == this.orderInfo.guaranteeType) {
                  this.baopeiSelectObj = { ...i };
                }
              });
            }
            return;
          } else {
            return;
          }
        });
    },
    // 获取当前租户的官方代售服务费配置
    getServiceFeeConfig() {
      this.serviceFeeConfig = {
        buyerRate: "",
        buyerMin: "",
        sellerRate: "",
        sellerMin: "",
      };
      customerServiceApi
        .getConfigByTenantId({
          configKey: "serviceFeeConfig",
        })
        .then((res) => {
          if (res.data && res.data.configValue) {
            this.serviceFeeConfig = JSON.parse(res.data.configValue);
            // this.computedActualAmount()
          }
        });
    },
    // 计算实付金额
    computedActualAmount() {
      // 商品金额
      let num = Number(this.ruleForm.saleAmount) 
        // (this.ruleForm.saleAmount *
        //   (!this.orderInfo.saleDiscount ||
        //   this.orderInfo.saleDiscount == "0.0" ||
        //   this.orderInfo.saleDiscount == 0
        //     ? 10
        //     : this.orderInfo.saleDiscount)) /
        // 10;

      // 包赔金额
      let baopei = 0;
      if (this.baopeiSelectObj) {
        const selectBaopei = this.baopeiSelectObj;
        // 买包赔
        let baopeifei = Math.round((num * selectBaopei.ratio) / 100);
        baopei =
          baopeifei > selectBaopei.minAmount
            ? baopeifei
            : Number(selectBaopei.minAmount);
      }
      this.ruleForm.guaranteeAmount = baopei;

      if (this.orderInfo.type == 3) {
        // C2C订单
        // 计算买家服务费
        let buyerFee = 0;
        if (this.serviceFeeConfig.buyerRate || this.serviceFeeConfig.buyerMin) {
          const buyerMin = Number(this.serviceFeeConfig.buyerMin||0)
          const rate = this.serviceFeeConfig.buyerRate
            ? this.serviceFeeConfig.buyerRate
            : 0;
          buyerFee = Math.round(num * rate / 100);
          buyerFee =
            buyerFee > buyerMin
              ? buyerFee
              : buyerMin;
        }
        this.ruleForm.servicePrice = buyerFee;
        // 计算卖家手续费
        let sellerFee = 0;
        if (
          this.serviceFeeConfig.sellerRate ||
          this.serviceFeeConfig.sellerMin
        ) {
          const sellerMin = Number(this.serviceFeeConfig.sellerMin||0)
          const rate = this.serviceFeeConfig.sellerRate
            ? this.serviceFeeConfig.sellerRate / 100
            : 0;
          sellerFee = Math.round(num * rate);
          sellerFee =
            sellerFee > sellerMin
              ? sellerFee
              : sellerMin;
        }
        this.ruleForm.sellerFee = sellerFee;
        this.ruleForm.supplyAmount = num - sellerFee;
      }

      // 计算应付总金额
      this.ruleForm.actualAmount =
        Math.round((num + baopei + this.ruleForm.servicePrice) * 100) / 100;
      return false;
    },
    // 计算实际结算价
    computedSupplyAmount() {
      let num =
        (this.ruleForm.supplyAmount *
          (!this.orderInfo.supplyDiscount ||
          this.orderInfo.supplyDiscount == "0.0" ||
          this.orderInfo.supplyDiscount == 0
            ? 10
            : this.orderInfo.supplyDiscount)) /
        10;
      this.ruleForm.supplyDiscountAmount =
        num < 0.01 ? 0 : Math.round(num * 100) / 100;
      return false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          orderApi.editOrderPrice(this.ruleForm).then((res) => {
            this.dialogVisible = false;
            this.loading = false;
            if (res.code == 200) {
              this.$parent.getList();
              this.$message.success("改价成功！");
            }
          });
        } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },
    close(formName) {
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.demo-ruleForm {
  padding-right: 20px;
}

.center {
  text-align: center;
}

.pick-dialog >>> .el-input--medium input {
  text-align: left;
}
</style>
